package net.ithe.controller;

import lombok.extern.slf4j.Slf4j;
import net.ithe.pojo.Emp;
import net.ithe.pojo.EmpQueryParam;
import net.ithe.pojo.PageResult;
import net.ithe.pojo.Result;
import net.ithe.service.EmpService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;

/**
 * 员工管理controller
 */
@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {
    @Autowired
    private EmpService empService;
    /**
     * 分页查询员工数据
     * @return
     */
//    @GetMapping
//    public Result page(@RequestParam(defaultValue = "1")Integer page,
//                       @RequestParam(defaultValue = "10") Integer pageSize,
//                       String name, Integer gender,
//                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin, @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){
//        log.info("分页查询员工数据,参数:{},{},{},{},{},{}",page,pageSize,name,gender,begin,end);
//        PageResult<Emp> pageResult=empService.page(page,pageSize,name,gender,begin,end);
//        return Result.success(pageResult);
//    }
    //================优化方案=================================
    @GetMapping
    public Result page(EmpQueryParam empQueryParam){
        log.info("分页查询员工数据,参数:{}",empQueryParam);
        PageResult<Emp> pageResult=empService.page(empQueryParam);
        return Result.success(pageResult);
    }
    @PostMapping
    public Result save(@RequestBody  Emp emp){
        log.info("保存员工数据,参数:{}",emp);
        empService.save(emp);
        return Result.success();
    }

    /**
     * 删除员工
     * @param ids
     * @return
     */
    @DeleteMapping
    public Result delete(@RequestParam List<Integer> ids){
        log.info("批量删除员工数据,参数:{}",ids);
        empService.delete(ids);
        return Result.success();
    }

    /**
     * 根据id查询员工
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Integer id){
        log.info("根据id查询员工数据,参数:{}",id);
        Emp emp=empService.getById(id);
        return Result.success(emp);
    }
    /**
     * 更新员工
     * @param emp
     * @return
     */
    @PutMapping
    public Result update(@RequestBody Emp emp){
        log.info("修改员工数据,参数:{}",emp);
        empService.update(emp);
        return Result.success();
    }
}
